package tank.po;

import tank.Enum.Dir;
import tank.Enum.Group;
import tank.util.Audio;
import tank.util.PropertyMgr;
import tank.util.ResourceMgr;

import java.awt.*;
import java.util.Random;

public class Tank {
    //定义坦克的位置
    private int x, y;
    //定义坦克的方向
    private Dir dir = Dir.DOWN;
    //设定速度  可以做贪吃蛇了
    final int SPEED = Integer.parseInt((String) PropertyMgr.get("tankSpeed"));
    //根据图片大小确定自己坦克大小
    public static final int WIDTH = ResourceMgr.goodTankU.getWidth();
    public static final int HEIGHT = ResourceMgr.goodTankU.getHeight();

    private Random random = new Random();

    //让他刚开始处于静止状态
    private boolean moving = true;
    //坦克拿到子弹的引用
    public TankFrame tf;
    //判断坦克是否存活
    private boolean living = true;
    //区分敌我坦克
    private Group group =Group.BAD;
    //碰撞会new出来很多rect  容易造成内存浪费
    Rectangle rect = new Rectangle();

    //setget方法
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }

    public boolean isMoving() {
        return moving;
    }
    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public Dir getDir() {
        return dir;
    }
    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public Group getGroup() {
        return group;
    }
    public void setGroup(Group group) {
        this.group = group;
    }

    //坦克的构造器
    public Tank(int x,int y, Dir dir, TankFrame tf,Group group) {
        super();
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
        this.group =group;

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;

    }
    //画坦克
    public void paint(Graphics g) {
        if(!living) tf.tanks.remove(this);
        switch (dir) {
            case LEFT:
                //g.drawImage(ResourceMgr.tankL, x, y, null);
                g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null);
                break;
            case UP:
                //g.drawImage(ResourceMgr.tankU, x, y, null);
                g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y, null);
                break;
            case DOWN:
                //g.drawImage(ResourceMgr.tankD, x, y, null);
                g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y, null);
                break;
            case RIGHT:
                //g.drawImage(ResourceMgr.tankR, x, y, null);
                g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y, null);
                break;
        }

//        //黑块坦克
//        Color c = g.getColor();
//        g.setColor(Color.YELLOW);
//        g.fillRect(x,y,50,50);
//        g.setColor(c);
        //判断他的方向
        move();
        //坦克的碰撞检测
        boundCheck();
        //update rect
        rect.x = this.x;
        rect.y = this.y;
    }
    //坦克移动
    private void move() {
        if (!moving) return;
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
        //敌人开火  随机开火
        if(this.group ==Group.BAD && random.nextInt(100)>95) this.fire();
        //自己开火
        if(this.group == Group.BAD && random.nextInt(100) > 95) randomDir();
    }
    //坦克的边界检测
    private void boundCheck() {
        if(this.x < 2) x = 2;
        if (this.y < 28) y = 28;
        if (this.x > TankFrame.GAME_WIDTH- Tank.WIDTH -2) x = TankFrame.GAME_WIDTH - Tank.WIDTH -2;
        if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT -2 ) y = TankFrame.GAME_HEIGHT -Tank.HEIGHT -2;
    }
    //设置自己开火的方法
    private void randomDir() {
        this.dir= Dir.values()[random.nextInt(4)];
    }
    //开火
    public void fire() {
        //计算子弹在坦克的什么位置
        int bX = this.x + Tank.WIDTH/2-Bullet.WIDTH/2;
        int bY = this.y + Tank.HEIGHT/2-Bullet.HEIGHT/2;
        tf.bullets.add(new Bullet(bX, bY, this.dir, this.tf,this.group));
        if(this.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
    }
    //判断生存状态
    public void die() {
        this.living = false;
    }
}
